package ca.swank.hardquor;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;

public class hardquorArrayListList extends JList
{
  private hardquorArrayListListModel model;

  public hardquorArrayListList()
  { super();
    model = new hardquorArrayListListModel();
    super.setModel(model);
  }

  public void addOptions(Object[] options)
  { model.addOptions(options);
  }

  public void removeOption(Object option)
  { model.removeOption(option);
  }

  public void addOption(Object option)
  { model.addOption(option);
  }

  public Object getSelectedOption()
  { int selected = getSelectedIndex();
    if(selected != -1) return model.getElementAt(selected);
    else return null;
  }
}

class hardquorArrayListListModel implements ListModel
{

  private ArrayList listOptions;
  private ArrayList listeners;

  public hardquorArrayListListModel()
  { listOptions = new ArrayList();
    listeners = new ArrayList();
  }

  public void addOptions(Object[] options)
  { int originalSize = listOptions.size();
	for(int i = 0; i < options.length; i++)
    { if(listOptions.indexOf(options[i]) == -1) listOptions.add(options[i]);
    }
    tellListeners(new ListDataEvent(this, javax.swing.event.ListDataEvent.INTERVAL_ADDED, originalSize, listOptions.size()));
  }

  public void removeOption(Object option)
  { int originalSize = listOptions.size();
	for(int i = 0; i < listOptions.size(); i++)
    { if(listOptions.get(i).equals(option)) listOptions.remove(i);
    }
    tellListeners(new ListDataEvent(this, javax.swing.event.ListDataEvent.INTERVAL_ADDED, originalSize, listOptions.size()));
  }

  public void addOption(Object option)
  { listOptions.add(option);
    tellListeners(new ListDataEvent(this, javax.swing.event.ListDataEvent.INTERVAL_ADDED, listOptions.size() - 1, listOptions.size()));
  }

  public void addListDataListener(ListDataListener l)
  { listeners.add(l);
  }

  public void removeListDataListener(ListDataListener l)
  { listeners.remove(listeners.indexOf(l));
  }

  public Object getElementAt(int i)
  { return listOptions.get(i);
  }

  public int getSize()
  { return listOptions.size();
  }

  public void tellListeners(ListDataEvent e)
  { for(int i = 0; i < listeners.size(); i++)
    { ListDataListener l = (ListDataListener)listeners.get(i);
      if(e.getType() == ListDataEvent.CONTENTS_CHANGED) l.contentsChanged(e);
      else if(e.getType() == ListDataEvent.INTERVAL_ADDED) l.intervalAdded(e);
      else if(e.getType() == ListDataEvent.INTERVAL_REMOVED) l.intervalRemoved(e);
    }
  }
}
